package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import com.itheima.domain.Vo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

//页面跳转
@Controller
public class UserController {
    @RequestMapping(value = "/test1")
    public ModelAndView sava1(){
         /*
            Model:模型 作用封装数据
            View：视图 作用展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","itcast");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/test2")
    public ModelAndView sava2(ModelAndView modelAndView){
       modelAndView.addObject("username","itheima");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/test3")
    public String sava3(Model model){
       model.addAttribute("username","好好学习");
       return "success";
    }

    @RequestMapping(value = "/test4")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String sava4(){
        return "hello itheima";
    }

    @RequestMapping(value = "/test5")
    public void sava5(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello itcast");
    }

    @RequestMapping(value = "/test6")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String sava6() throws IOException {
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }

    @RequestMapping(value = "/test7")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String sava7() throws JsonProcessingException {
        User user =new User();
        user.setUsername("lisi");
        user.setAge(18);
        //使用json的转换工具将对象转换成json格式字符串在返回
        ObjectMapper objectMapper =new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping(value = "/test8")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public User sava8() throws IOException {
        User user =new User();
        user.setUsername("lisi2");
        user.setAge(19);
        return user;
    }

    @RequestMapping(value = "/test9")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public void sava9(String username,int age){
        System.out.println(username);
        System.out.println(age);
    }

    @RequestMapping(value = "/test10")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public void sava10(User user){
        System.out.println(user);
    }

    @RequestMapping(value = "/test11")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public void sava11(String[] str){
        System.out.println(Arrays.asList(str));
    }

    @RequestMapping(value = "/test12")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public void sava12(Vo vo){
        System.out.println(Arrays.asList(vo));
    }

    @RequestMapping(value = "/test13")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public void sava13(@RequestBody List<User> userList){
        System.out.println(Arrays.asList(userList));
    }


    @RequestMapping(value = "/test21")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public void sava21(String username, MultipartFile uploadFile){
        System.out.println(username);
        System.out.println(uploadFile);
    }

    @RequestMapping(value = "/test22")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public void sava22(String username, MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("C:\\upload\\"+originalFilename));
    }

    @RequestMapping(value = "/test23")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public void sava23(String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println(username);
        for (MultipartFile multipartFile : uploadFile){
            String originalFilename = multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File("C:\\upload\\"+originalFilename));
        }
    }

}
